home *** CD-ROM | disk | FTP | other *** search
/ Freelog 15 / FREELOG 15.ISO / WebMaster / Perl / PERL5106.ZIP / perl5 / Lib / Win32 / REGISTRY.PM < prev    next >
Encoding:
Perl POD Document  |  1996-02-28  |  9.4 KB  |  486 lines

  1. package Win32::Registry;
  2. #######################################################################
  3. #Perl Module for Registry Extensions
  4. # This module creates an object oriented interface to the Win32
  5. # Registry.
  6. #
  7. # NOTE: This package exports four instantiated keys to
  8. # the main:: name space.  ( The pre-defined keys )
  9. # these keys:
  10. # $main::CLASSES_ROOT
  11. # $main::CURRENT_USER
  12. # $main::LOCAL_MACHINE
  13. # $main::USERS
  14. #
  15. #######################################################################
  16.  
  17. require Exporter;       #to export the constants to the main:: space
  18. require DynaLoader;     # to dynuhlode the module.
  19. use Win32::WinError;         # for windows constants.
  20.  
  21.  
  22. @ISA= qw( Exporter DynaLoader );
  23. @EXPORT = qw(
  24.     HKEY_CLASSES_ROOT
  25.     HKEY_CURRENT_USER
  26.     HKEY_LOCAL_MACHINE
  27.     HKEY_PERFORMANCE_DATA
  28.     HKEY_PERFORMANCE_NLSTEXT
  29.     HKEY_PERFORMANCE_TEXT
  30.     HKEY_USERS
  31.     KEY_ALL_ACCESS
  32.     KEY_CREATE_LINK
  33.     KEY_CREATE_SUB_KEY
  34.     KEY_ENUMERATE_SUB_KEYS
  35.     KEY_EXECUTE
  36.     KEY_NOTIFY
  37.     KEY_QUERY_VALUE
  38.     KEY_READ
  39.     KEY_SET_VALUE
  40.     KEY_WRITE
  41.     REG_BINARY
  42.     REG_CREATED_NEW_KEY
  43.     REG_DWORD
  44.     REG_DWORD_BIG_ENDIAN
  45.     REG_DWORD_LITTLE_ENDIAN
  46.     REG_EXPAND_SZ
  47.     REG_FULL_RESOURCE_DESCRIPTOR
  48.     REG_LEGAL_CHANGE_FILTER
  49.     REG_LEGAL_OPTION
  50.     REG_LINK
  51.     REG_MULTI_SZ
  52.     REG_NONE
  53.     REG_NOTIFY_CHANGE_ATTRIBUTES
  54.     REG_NOTIFY_CHANGE_LAST_SET
  55.     REG_NOTIFY_CHANGE_NAME
  56.     REG_NOTIFY_CHANGE_SECURITY
  57.     REG_OPENED_EXISTING_KEY
  58.     REG_OPTION_BACKUP_RESTORE
  59.     REG_OPTION_CREATE_LINK
  60.     REG_OPTION_NON_VOLATILE
  61.     REG_OPTION_RESERVED
  62.     REG_OPTION_VOLATILE
  63.     REG_REFRESH_HIVE
  64.     REG_RESOURCE_LIST
  65.     REG_RESOURCE_REQUIREMENTS_LIST
  66.     REG_SZ
  67.     REG_WHOLE_HIVE_VOLATILE
  68. );
  69.  
  70. #######################################################################
  71. # This AUTOLOAD is used to 'autoload' constants from the constant()
  72. # XS function.  If a constant is not found then control is passed
  73. # to the AUTOLOAD in AutoLoader.
  74.  
  75. sub AUTOLOAD {
  76.     local($constname);
  77.     ($constname = $AUTOLOAD) =~ s/.*:://;
  78.     $val = constant($constname, @_ ? $_[0] : 0);
  79.     if ($! != 0) {
  80.     if ($! =~ /Invalid/) {
  81.         $AutoLoader::AUTOLOAD = $AUTOLOAD;
  82.         goto &AutoLoader::AUTOLOAD;
  83.     }
  84.     else {
  85.         ($pack,$file,$line) = caller;
  86.         die "Your vendor has not defined Registry macro $constname, used at $file line $line.
  87. ";
  88.     }
  89.     }
  90.     eval "sub $AUTOLOAD { $val }";
  91.     goto &$AUTOLOAD;
  92. }
  93.  
  94. #######################################################################
  95. # _new is a private constructor, not intended for public use.
  96. #
  97.  
  98. sub show_me
  99. {
  100.     $self=shift;
  101.     print $self->{'handle'};
  102. }
  103.  
  104. sub _new
  105. {
  106.     my $self={};
  107.     if ($_[0]){
  108.         $self->{'handle'} = $_[0];
  109.         bless $self
  110.         }
  111.     else{
  112.             undef($self);
  113.     }
  114.     $self;
  115. }
  116.  
  117. #define the basic registry objects to be exported.
  118. #these had to be hardwired unfortunately.
  119.  
  120.  
  121. $main::HKEY_CLASSES_ROOT = _new(0x80000000);
  122. $main::HKEY_CURRENT_USER = _new(0x80000001);
  123. $main::HKEY_LOCAL_MACHINE = _new(0x80000002);
  124. $main::HKEY_USERS = _new(0x80000003);
  125. $main::HKEY_PERFORMANCE_DATA = _new(0x80000004 );
  126. $main::HKEY_PERFORMANCE_TEXT =_new(0x80000050 );
  127. $main::HKEY_PERFORMANCE_NLSTEXT =_new(0x80000060 );
  128.  
  129.  
  130.  
  131.  
  132.  
  133. #######################################################################
  134. #Open: creates a new Registry object from an existing one.
  135. # usage: $RegObj->Open( "SubKey",$SubKeyObj );
  136. #               $SubKeyObj->Open( "SubberKey", *SubberKeyObj );
  137.  
  138. sub Open
  139. {
  140.     my $self = shift;
  141.     
  142.     if( $#_ != 1 ){
  143.         die 'usage: Open( $SubKey, $ObjRef )';
  144.     }
  145.     
  146.     ($SubKey) = @_;
  147.     local $Result,$SubHandle;
  148.  
  149.     $Result = RegOpenKey($self->{'handle'},$SubKey,$SubHandle);
  150.     $_[1] = _new( $SubHandle );
  151.     
  152.     if (!$_[1] ){
  153.         return 0;
  154.     }
  155.  
  156.      if(!$Result){
  157.         $! = Win32::GetLastError();
  158.     }
  159.  
  160.     # return a boolean value
  161.     return($Result);
  162.  
  163. }
  164.  
  165. #######################################################################
  166. #Close
  167. # close an open registry key.
  168. #
  169. sub Close
  170. {
  171.     my $self = shift;
  172.     
  173.     if( $#_ != -1 ){
  174.         die "usage: Close()";
  175.     }
  176.  
  177.     $Result = RegCloseKey( $self->{'handle'});
  178.     undef($self);
  179.  
  180.     if(!$Result){
  181.         $! = Win32::GetLastError();
  182.     }
  183.  
  184.     return($Result);
  185. }
  186.  
  187.  
  188. #######################################################################
  189. #Create
  190. # open a subkey.  If it doesn't exist, create it.
  191. #
  192.  
  193. sub Create
  194. {
  195.     my $self = shift;
  196.  
  197.     if($#_ != 1 ){
  198.         die 'usage: Create( $SubKey,$ScalarRef )';
  199.     }
  200.  
  201.     ($SubKey) = @_;
  202.     local $Result,$SubHandle;
  203.  
  204.     #call the API, and create the object.
  205.     $Result = RegCreateKey($self->{'handle'},$SubKey,$SubHandle);
  206.     $_[1] = _new ( $SubHandle );
  207.     if (!$_[1]){
  208.         return(0);
  209.     }
  210.     #error checking
  211.  
  212.      if(!$Result){
  213.         $! = Win32::GetLastError();
  214.     }
  215.  
  216.     return($Result);
  217.  
  218. }
  219.  
  220. #######################################################################
  221. #SetValue
  222. # SetValue sets a value in the current key.
  223. #
  224.  
  225. sub SetValue
  226. {
  227.     my $self = shift;
  228.     if($#_ != 2 ){
  229.         die 'usage: SetValue($SubKey,$Type,$value )';
  230.     }
  231.  
  232.     local($SubKey,$type,$value) = @_;
  233.  
  234.     # set the value.
  235.     $Result = RegSetValue( $self->{'handle'},$SubKey,$type,$value);
  236.     
  237.      if(!$Result){
  238.         $! = Win32::GetLastError();
  239.     }
  240.  
  241.     return($Result);
  242.  
  243. }
  244.  
  245. sub SetValueEx
  246. {
  247.     my $self = shift;
  248.     if($#_ != 3){
  249.         die 'usage: SetValueEx( $SubKey,$Reserved,$type,$value )';
  250.     }
  251.  
  252.     local( $SubKey,$Reserved,$type,$value) =@_;
  253.  
  254.     $Result = RegSetValueEx( $self->{'handle'},$SubKey,$Reserved,$type,$value);
  255.     
  256.     if(!$Result){
  257.         $! = Win32::GetLastError();
  258.     }
  259.  
  260.     return($Result);
  261. }
  262.  
  263. #######################################################################
  264. #QueryValue  and QueryKey
  265. # QueryValue gets information on a value in the current key.
  266. # QueryKey "    "       "       "  key  "       "       "       
  267.  
  268. sub QueryValue
  269. {
  270.     my $self = shift;
  271.  
  272.     if($#_ != 1 ){
  273.         die 'usage: QueryValue( $SubKey,$valueref )';
  274.     }
  275.  
  276.     #Query the value.
  277.     $Result = RegQueryValue( $self->{'handle'},$_[0],$_[1]);
  278.  
  279.     #check the results.
  280.  
  281.      if(!$Result){
  282.         $! = Win32::GetLastError();
  283.     }
  284.  
  285.     return($Result);
  286. }
  287.  
  288. sub QueryKey
  289. {
  290.     my $self = shift;
  291.  
  292.     if($#_ != 2 ){
  293.         die 'usage: QueryKey( $classref,$numberofSubkeys,$numberofVals )';
  294.     }
  295.  
  296.     local $Result;
  297.  
  298.     $Result = RegQueryInfoKey( $self->{'handle'},$_[0],
  299.                    $garbage,$garbage,$_[1],
  300.                    $garbage,$garbage,$_[2],
  301.                    $garbage,$garbage,$garbage,$garbage );
  302.  
  303.  
  304.      if(!$Result){
  305.         $! = Win32::GetLastError();
  306.     }
  307.     return($Result);
  308. }
  309.  
  310. #######################################################################
  311. #GetKeys
  312. #Note: the list object must be passed by reference: 
  313. #       $myobj->GetKeys( \@mylist )
  314. sub GetKeys
  315. {
  316.     my $self = shift;
  317.     if($#_ != 0 ){
  318.         die 'usage: GetKeys( $arrayref )';
  319.     }
  320.  
  321.     if (ref $_[0] != ARRAY){
  322.         die "GetKeys requires a list reference as an arguement";
  323.     }
  324.  
  325.     local $Result,$ValueName,$i,$hkey,$keyname;
  326.  
  327.     $ValueName="DummyVal";$i=0;
  328.     $Result = 1;
  329.     
  330.     while( $Result ){
  331.         $Result = RegEnumKey( $self->{'handle'},$i++, $keyname );
  332.         if ($Result){
  333.             push( @{$_[0]}, $keyname );
  334.         }
  335.     }
  336.     return(1);
  337.  
  338. }
  339. #######################################################################
  340. #GetValues
  341. # GetValues creates a hash containing 'name'=> ( name,type,data )
  342. # for each value in the current key.
  343.  
  344. sub GetValues
  345. {
  346.     my $self = shift;
  347.  
  348.     if($#_ != 0 ){
  349.         die 'usage: GetValues( $hashref )';
  350.     }
  351.  
  352.     local $Result,$ValueName,$i;
  353.  
  354.     $ValueName="DummyVal";$i=0;
  355.     while( $Result=RegEnumValue( $self->{'handle'},
  356.                     $i++,
  357.                     $ValueName,
  358.                     NULL,
  359.                     $ValueType,
  360.                     $ValueData )){
  361.  
  362.         $aref = [ $ValueName, $ValueType,$ValueData ];
  363.  
  364.         $_[0]->{$ValueName} = $aref;
  365.     }
  366.         
  367.     return(1);
  368. }
  369.  
  370. #######################################################################
  371. #DeleteKey
  372. # delete a key from the registry.
  373. #  eg: $CLASSES_ROOT->DeleteKey( "KeyNameToDelete");
  374. #
  375.  
  376. sub DeleteKey
  377. {
  378.     my $self = shift;
  379.     local($Result);
  380.     if($#_ != 0 ){
  381.         die 'usage: DeleteKey( $SubKey )';
  382.     }
  383.  
  384.     local( $name ) = @_;
  385.  
  386.     $Result=RegDeleteKey($self->{'handle'},$name);
  387.  
  388.      if(!$Result){
  389.         $! = Win32::GetLastError();
  390.     }
  391.     return($Result);
  392.  
  393. }
  394. #######################################################################
  395. #DeleteValue
  396. # delete a value from the current key in the registry
  397. #  $CLASSES_ROOT->DeleteValue( "\000" );
  398.  
  399. sub DeleteValue
  400. {
  401.     my $self = shift;
  402.     local( $Result );
  403.  
  404.     if($#_ != 0 ){
  405.         die 'usage: DeleteValue( $SubKey )';
  406.     }
  407.  
  408.     local( $name )=@_;
  409.     
  410.     $Result=RegDeleteValue( $self->{'handle'},$name);
  411.     
  412.     if( !$Result){
  413.         $!=Win32::GetLastError();
  414.     }
  415.  
  416.     return($Result);
  417.  
  418. }
  419.  
  420. #######################################################################
  421. #save
  422. #saves the current hive to a file.
  423. #
  424.  
  425. sub Save
  426. {
  427.     my $self=shift;
  428.  
  429.     if($#_ != 0 ){
  430.         die 'usage: Save( $FileName )';
  431.     }
  432.  
  433.     local( $FileName ) = @_;
  434.  
  435.     $Result=RegSaveKey( $self->{'handle'},$FileName );
  436.  
  437.     if( !$Result){
  438.         $!=Win32::GetLastError();
  439.     }
  440.  
  441.     return($Result);
  442. }
  443.  
  444. #######################################################################
  445. #Load
  446. #loads a saved key from a file.
  447.  
  448. sub Load
  449. {
  450.     my $self = shift;
  451.     if($#_ != 1 ){
  452.         die 'usage: Load( $SubKey,$FileName )';
  453.     }
  454.  
  455.     local( $SubKey,$FileName) = @_;
  456.  
  457.     $Result=RegLoadKey( $self->{'handle'},$SubKey,$FileName);
  458.  
  459.     if( !$Result){
  460.         $!=Win32::GetLastError();
  461.     }
  462.  
  463.     return($Result);
  464. }
  465. #######################################################################
  466. # dynamically load in the Registry.pll module.
  467.  
  468.  
  469. bootstrap Win32::Registry;
  470.  
  471. # Preloaded methods go here.
  472.  
  473. #Currently Autoloading is not implemented in Perl for win32
  474. # Autoload methods go after __END__, and are processed by the autosplit program.
  475.  
  476. 1;
  477. __END__
  478.  
  479.  
  480.  
  481.  
  482.  
  483.     
  484.  
  485.     
  486.